package net.cjw.initsb.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public class ServiceResult<T> {

	private int code;
	private String msg;
	private Long count;// layui 数据表格使用
	private T data;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public ServiceResult(int code, String msg, T data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public ServiceResult(int code, String msg, Long count, T data) {
		super();
		this.code = code;
		this.msg = msg;
		this.count = count;
		this.data = data;
	}

	public static <T> ServiceResult<T> buildSuccess(T data) {
		return new ServiceResult<T>(ServiceResultCode.SUCCESS, null, data);
	}

	public static <T> ServiceResult<T> buildSuccess(T data, Long count) {
		return new ServiceResult<T>(ServiceResultCode.SUCCESS, "", count, data);
	}

	public static <T> ServiceResult<T> buildSuccess() {
		return new ServiceResult<T>(ServiceResultCode.SUCCESS, null, null);
	}

	public static <T> ServiceResult<T> buildNotLogin() {
		return new ServiceResult<T>(ServiceResultCode.NOT_LOGIN, ServiceResultMsg.NOT_LOGIN_MSG, null);
	}

	public static <T> ServiceResult<T> buildError(String msg) {
		return new ServiceResult<T>(ServiceResultCode.ERROR, msg, null);
	}

	public static <T> ServiceResult<T> buildNotPermission() {
		return new ServiceResult<T>(ServiceResultCode.NOT_PERMISSION, ServiceResultMsg.NOT_PERMISSION_MSG, null);
	}

	@JsonIgnore
	public boolean isSuccess() {
		return this.code == 1;

	}

	public interface ServiceResultCode {
		int ERROR = 0;
		int SUCCESS = 1;
		int NOT_LOGIN = 2;
		int NOT_PERMISSION = 3;
	}

	public interface ServiceResultMsg {
		String SERVER_ERROR_MSG = "服务器异常";
		String REQUEST_PARAM_ERROR_MSG = "传参错误";
		String VERSION_ERROR_MSG = "该版本不存在或放弃维护";
		String LOGIN_ERROR_MSG = "账号或或密码错误";
		String NOT_LOGIN_MSG = "没有登录";
		String NOT_PERMISSION_MSG = "没有权限";
	}

	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
	}

}
